package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    //新增菜品，同时也要增加口味
    //这个操作涉及到两个表，要保证事务的一致性
    @Transactional
    @Override
    public void save(DishDTO dishDTO) {
        //创建一个菜品的实体类
        Dish dish = new Dish();
        //将DTO中的数据拷贝到实体类中,只有dish实体类中存在的有相同的字段才会进行拷贝
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品表插入一条数据
        dishMapper.insert(dish);
        //获取Insert生成的菜品id
        Long dishId = dish.getId();
        //获取口味的数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //向口味表插入多条数据
        if(flavors != null && flavors.size() > 0){
            //遍历所有的口味，给它的菜品id进行赋值操作
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    //菜品分页查询
    @Override
    public PageResult queryByPage(DishPageQueryDTO dishPageQueryDTO) {
        //使用第三方PageHelper进行分页查询
        int page = dishPageQueryDTO.getPage();
        int pageSize = dishPageQueryDTO.getPageSize();
        //开始启动
        PageHelper.startPage(page,pageSize);
        //进行正常的访问数据库，并返回集合
        List<DishVO> dishVOList = dishMapper.queryByPage(dishPageQueryDTO);
        //对获得数据进行PageInfo封装
        PageInfo<DishVO> pageInfo = new PageInfo<>(dishVOList);
        //对将要输出的PageResult进行封装
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getList());
        //将pageResult进行返回
        return pageResult;
    }

    //批量删除菜品
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        //判断该商品中是否有起售的商品，如果有起售的商品则不能进行删除
        for (Long id : ids) {
           Dish dish = dishMapper.getDishById(id);
           if(dish.getStatus() == StatusConstant.ENABLE){
               throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
           }
        }
        //判断当前菜品是否被套餐关联，如果被套餐关联，则也不能进行删除
       List<Long> setMealIds = setmealDishMapper.getSetMealByDishIds(ids);
       if(setMealIds != null && setMealIds.size() > 0){
           throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
       }
       //删除菜品表中菜品数据,删除对应的口味表
        for (Long id : ids) {
            dishMapper.deleteById(id);
            dishFlavorMapper.deleteDishFlavorById(id);
        }
    }

    @Override
    public DishVO queryByIdWithFlavor(Long id) {
        //根据id查询菜品数据
        Dish dish = dishMapper.getDishById(id);
        //根据id查询口味数据
        List<DishFlavor>  dishFlavorList = dishFlavorMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    @Transactional
    @Override
    public void updateById(DishDTO dishDTO) {
        //根据id修改菜品信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateById(dish);
        //根据id删除口味信息
        dishFlavorMapper.deleteDishFlavorById(dishDTO.getId());
        //根据id插入口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            //遍历所有的口味，给它的菜品id进行赋值操作
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    //菜品起售与停售
    @Override
    public void startOrStop(Integer status, Long id) {
        //创建一个菜品的实体类
        Dish dish = Dish.builder().status(status).id(id).build();
        dishMapper.updateById(dish);
    }

    @Override
    public List<Dish> queryByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.queryByCategoryId(categoryId);
        return dishList;
    }

    @Override
    public List<DishVO> queryByCategoryIdWithFlavor(Long categoryId) {
        //根据分类id查询菜品数据
        List<DishVO> dishVOList = dishMapper.queryByCategoryIdWithFlavor(categoryId);
        //遍历菜品，查找给一个菜品的口味
        for (DishVO dishVO : dishVOList) {
            Long dish_id = dishVO.getId();
            //根据菜品id查询口味数据
            List<DishFlavor> dishFlavorList = dishFlavorMapper.getById(dish_id);
            //将查询到的口味数据封装到VO中
            dishVO.setFlavors(dishFlavorList);
         }
        //然后将数据进行返回
        return dishVOList;
    }
}
